#include "gpio_control.h"

#include <unistd.h>
#include <cstdlib>
//第三方库
#include <curl/curl.h>

//http协议 获取数据
int poweroff(const char* url){
    CURL *curl;
    CURLcode res;
    std::string temp;

    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();
    curl_global_cleanup();

    return 0;
}

//主函数：一直检测
int main(){

    std::cout <<"电源开关程序~~~"<< std::endl;

    int switch_pin=39;
    int switch_state=0;//开关状态
    double time=0.1;

    //初始化引脚
    gpio_control power_switch = gpio_control(switch_pin);
    power_switch.gpio_direction("in");
    power_switch.gpio_edge("none");
    power_switch.gpio_edge("rising");

    //主线程
    while (1)
    {
        /* code */
        switch_state = power_switch.get_gpio_outvalue();
        // printf(">>> 开关状态：%d\n\r");

        if(switch_state == 1){
            //按键按下
            time=1;
            std::cout << "关机了~~~" << std::endl;

            //通知102关机
            poweroff("http://10.222.222.102/poweroff");

            //执行关机指令
            system("poweroff");
            

        }else{
            time=0.1;
        }

        sleep(time);
    }
    return 0;
}
